#!/usr/bin/perl
#fileter.cgi

require './shareman-lib.pl';

&ReadParse();
#--------New Text---------#
$text{'filter'} = "File Filter";
$text{'sharedir'} = "Share Path";
#-------------------------#
#&header($text{'filter'}, "", "index", 0, 1);
my $ft = $in{'filtertype'};
@tp=("N", "A", "D");
$ft=$tp[$ft];
my $path = $in{'sharepath'};
my $sharename = $in{'sharename'};
my $sharetype = $in{'sharetype'};
my @type=split(" ", $in{'filetype'});
my $n=@type;
my $ts;
my $veto;
for($i=0; $i<$n; $i++){
	if($i == 0){
		$ts = $type[$i];
		$veto = "\/\*\.".$type[$i]."\/"
	}
	else{
		$ts .= ",".$type[$i];
		$veto .= "\*\.".$type[$i]."\/";
	}
}
if($ft eq "N"){
	$fstring="";
}
else{
	$fstring=$path.":".$ft.":".$ts;
}
#print $fstring;
open(FH, "/sysvol/etc/filter.conf");
@fconf=<FH>;
close(FH);

$num = @fconf;
$found = 0;
$k=$num;
for ($j=0; $j<$num; $j++) {

	$fconf[$j] =~ s/\r|\n//g;
	@fcstr = split(":", $fconf[$j]);
	if($fcstr[0] eq $path){
		$found=1;
		$k=$j;
	}
}
$fconf[$k]=$fstring;
#print "<br>".$k;
$nn=@fconf;
#print "<br>".$nn;
open(FB, ">/sysvol/etc/filter.conf");
for($m=0; $m<$nn; $m++){
	if($fconf[$m] ne ""){
		print FB $fconf[$m]."\n";
	}
}
close(FB);
if($sharetype =~ /W/){
if($ft eq "D"){
	&foreign_require("samba", "samba-lib.pl");
	&foreign_call("samba", "get_share", $sharename);
	&foreign_call("samba", "setval", "veto files", $veto);
	&foreign_call("samba", "modify_share", $sharename, $sharename);
}
else{
        &foreign_require("samba", "samba-lib.pl");
        &foreign_call("samba", "get_share", $sharename);
        &foreign_call("samba", "delval", "veto files");
        &foreign_call("samba", "modify_share", $sharename, $sharename);

}
system("/etc/init.d/smb restart");
}
&redirect("index.cgi")
#&footer("index.cgi", $text{'index'});
